-module(util_type_test).

-include_lib("eunit/include/eunit.hrl").


to_atom_test() ->
    ?assertEqual(hello, util_type:to_atom(hello)), %atom
    ?assertEqual(hello, util_type:to_atom(<<"hello">>)), %binary
    ?assertEqual('9', util_type:to_atom(9)), %integer
    ?assertEqual(hello, util_type:to_atom({$h, $e, $l, $l, $o})), %tuple
    ?assertEqual(hello, util_type:to_atom("hello")), %list
    ok.

to_list_test() ->
    ?assertEqual("hello", util_type:to_list("hello")), %list
    ?assertEqual("hello", util_type:to_list(hello)), %atom
    ?assertEqual("hello", util_type:to_list(<<"hello">>)), %binary
    ?assertEqual("9", util_type:to_list(9)), %integer
    %?assertEqual("9.9", util_type:to_list(9.9)), %float
    ok.

to_binary_test() ->
    ?assertEqual(<<"hello">>, util_type:to_binary(<<"hello">>)), %binary
    ?assertEqual(<<"hello">>, util_type:to_binary(hello)), %atom
    ?assertEqual(<<"hello">>, util_type:to_binary("hello")), %list
    ?assertEqual(<<"9">>, util_type:to_binary(9)), %integer
    %?assertEqual(<<"9.9">>, util_type:to_binary(9.9)), %float
    ok.

%%to_float_test() ->
%%    ?assertEqual(9.9, util_type:to_float(9.9)), %float
%%    ?assertEqual(9.9, util_type:to_float(<<"9.9">>)), %binary
%%    ?assertEqual(9.9, util_type:to_float("9.9")), %list
%%    ok.

to_integer_test() ->
    ?assertEqual(9, util_type:to_integer(9)), %integer
    ?assertEqual(9, util_type:to_integer(<<"9">>)), %binary
    ?assertEqual(9, util_type:to_integer("9")), %list
    ok.


term_to_bitstring_test() ->
    ?assertEqual(<<"[{name,mirahs},{age,18}]">>, util_type:term_to_bitstring([{name, mirahs}, {age, 18}])),
    ?assertEqual({ok, [{name,mirahs},{age,18}]}, util_type:bitstring_to_term(<<"[{name,mirahs},{age,18}]">>)),
    ok.


ip_to_bitstring_test() ->
    ?assertEqual(<<"127.0.0.1">>, util_type:ip_to_bitstring({127, 0, 0, 1})), %tuple
    ?assertEqual(<<"127.0.0.1">>, util_type:ip_to_bitstring(127, 0, 0, 1)), %integer
    ok.
